(function($){
		$.fn.serializeObject = function(){
		    var o = {};
		    var a = this.serializeArray();
		    $.each(a, function() {
		        if (o[this.name] !== undefined) {
		            if (!o[this.name].push) {
		                o[this.name] = [o[this.name]];
		            }
		            o[this.name].push(this.value || '');
		        } else {
		            o[this.name] = this.value || '';
		        }
		    });
		    return o;
		};
		
		/**
		 * 弹出窗口
		 * winId:窗口的ID, 必选 
		 */
		$.openWin = function(options){
			var optionDefault = {
					width:'600px',
				    modal:true,
				    collapsible:false,
				    minimizable:false,
				    maximizable:false,
				    iconCls:'icon-save',
				    style:{
				    	minWidth:'500px'
				    },
				    onClose:function(){
				    	$.closeWin(options.gridId);
				    }
			}
			var settings = $.extend( {}, optionDefault, options );
			var winId = settings.gridId+'_pop_win';
			var winDiv = '<div id="'+winId+'" modal="true" shadow="false" minimizable="false" cache="false" maximizable="false" collapsible="false" resizable="true" style="margin: 0px;padding: 0px;overflow: auto;"></div>';
			jQuery("body").append(winDiv);
			var popWin = jQuery("#"+winId).window(settings);
			var t = popWin.panel("options").top;
			t= t/2;
			popWin.window('resize',{top:t});
		};
		
		/**
		 * 关闭窗口
		 */
		$.closeWin = function(gridId){
			var winId = gridId+'_pop_win';
			$("#"+winId).parent().next().remove();
			$("#"+winId).window('destroy',true);
		}
		/**
		 * 弹出窗口
		 * 消息提示
		 */
		$.showMsg = function(options){
			var defaultOptions = {
					title:'系统消息',
					msg:'消息将在5秒后关闭。',
					width:'20%',
					height:'20%',
					timeout:1500,
					showType:'slide',
					ok:'确定',
					modal:true,
					style:{
						top:$(window).height()/2-(($(window).height()*20/100)/2),
					}	
			}
			var settings = $.extend({}, defaultOptions, options);
			$.messager.show(settings);
		}
		
		$.alert = function(msg,status,fn){
			switch(status){
				case 'y':
				case 'Y':
				case '0':
					$.messager.alert('系统提示',msg, 'info',fn);
					break;
				case 'n':
				case 'N':
				case '1':
					$.messager.alert('系统提示',msg, 'error',fn);
					break;
				case 'w':
				case 'W':
				case '2':
					$.messager.alert('系统提示',msg, 'warning',fn);
					break;
				case 'q':
				case 'Q':
				case '3':
					$.messager.alert('系统提示',msg, 'question',fn);
					break;
				default:
					$.messager.alert('系统提示',msg, 'info',fn);
					break;
			}
		};
		
		/**
		 * gridId: 表单提交之后要刷新的gridId
		 * url:请求地址
		 * callback:如果不需要关闭窗口. 或者有其他操作. 可以在callback中给出具体的执行过程. 
		 * 		参数:data, 将响应的信息传递给callback
		 */
		$.formSubmit = function(gridId, url, callback){
			$.messager.progress({ 
				title:'请稍后', 
				msg:'页面加载中...'
			});	// 显示进度条
			$('#'+gridId+'_form').form('submit', {
				url:url,
				onSubmit: function(){
					var isValid = $(this).form('validate');
					if (!isValid){
						$.messager.progress('close');	// 如果表单是无效的则隐藏进度条
					}
					return isValid;	// 返回false终止表单提交
				},
				success: function(data){
					$.messager.progress('close');	// 如果提交成功则隐藏进度条
					var ret = eval('('+data+')');
					if(callback!=null && callback != undefined){
						callback(ret);
					} else {
						$.closeWin(gridId);//关闭窗口
						var status = "";
						if(ret.success=='true'){
							status = 'y';
						} else {
							status = 'n';
						}
						$.alert(ret.msg,status,function(){
							doSearch(gridId);//刷新
						});
					}
				}
			});
		}
		$.doDelete = function(gridId, url, data, callback){
			$.messager.confirm('确认','您确认想要删除记录吗？',function(r){   
				if (r){    
					var idField = $menu_grid.datagrid("options").idField;
					var row = $menu_grid.datagrid("getChecked");
					var ids = "";
					for(var a in row){
						ids+=row[a][idField]+",";
					}
					ids = ids.substring(0,ids.length-1);
					if(ids == ''){
						$.alert('请选择一个要删除的数据','N');
						return false;
					} else {
						data.ids = ids;
					}
					$.messager.progress({ 
						title:'请稍后', 
						msg:'页面加载中...'
					});	// 显示进度条
					$.ajax({
						url:url,
						type:'post',
						data:data,
						success:function(msg){
							$.messager.progress('close');	// 如果提交成功则隐藏进度条
							var ret = msg;  // 为什么会这样??
							if(callback!=null && callback != undefined){
								callback(ret);
							} else {
								var status = "";
								if(ret.success=='true'){
									status = 'y';
								} else {
									status = 'n';
								}
								$.alert(ret.msg,status,function(){
									doSearch(gridId);//刷新
								});
							}
						}
					});
			    }    
			});  
		}
})(jQuery);


/**
 * 查询方法
 */
function doSearch(jqId){
	var data = $("#"+jqId+"_search_form").serializeObject();
	$("#"+jqId).datagrid('load',data);
}
